package com.tom.blog.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Entity //jpa实体
public class User implements Serializable, UserDetails {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Id //主键
	@GeneratedValue(strategy=GenerationType.IDENTITY) //自增
	private Long id;
	
	@NotEmpty(message="姓名不能为空")
	@Size(min=2, max=60, message="邮箱长度2-60个字符")
	@Column(nullable=false, length=60)
	private String name;
	
	@NotEmpty(message="邮箱不能为空")
	@Size(min=2, max=20, message="邮箱长度3-20个字符")
	@Email(message="邮箱格式不正确")
	@Column(nullable=false, length=50, unique=true)
	private String email;
	
	@NotEmpty(message="登录名不能为空")
	@Size(min=3, max=20, message="登录名长度3-20个字符")
	@Column(nullable=false, length=20, unique=true)
	private String username;
	
	@NotEmpty(message="密码不能为空")
	@Size(min=3, max=100, message="密码6-16个字符")
	@Column(length=100)
	private String password;

	@Column(length=300)
	private String avatar;//头像
	
	//用户-权限 多对多
	@ManyToMany(cascade=CascadeType.DETACH, fetch=FetchType.EAGER)
	@JoinTable(name="user_authority", 
	joinColumns = @JoinColumn(name="user_id", referencedColumnName="id"),
	inverseJoinColumns=@JoinColumn(name="authority_id", referencedColumnName="id")
	)
	private List<Authority> authorities;
	
	
	public User() {
	}

	public User(Long id, String name, String email) {
		this.id = id;
		this.name = name;
		this.email = email;
	}
	
	
	public User(Long id, String name, String email, String username, String password, String avatar) {
		super();
		this.id = id;
		this.name = name;
		this.email = email;
		this.username = username;
		this.password = password;
		this.avatar = avatar;
	}

	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getAvatar() {
		return avatar;
	}

	public void setAvatar(String avatar) {
		this.avatar = avatar;
	}


	public void setAuthorities(List<Authority> authorities) {
		this.authorities = authorities;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", email=" + email + ", username=" + username + ", password="
				+ password + ", avatar=" + avatar + "]";
	}

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		//为了能拿到角色名, 要转化成simpleGrantedAutority
		List<SimpleGrantedAuthority> authes = new ArrayList<>();
		this.authorities.forEach(a->authes.add(new SimpleGrantedAuthority(a.getAuthority())));
		return authes;
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return true;
	}

	/**
	 * 加密明文密码
	 * @param password
	 */
	public void setEncodePassword(String password) {
		PasswordEncoder  encoder = new BCryptPasswordEncoder();
		String encodePasswd = encoder.encode(password);
		this.password = encodePasswd;
	}
}
